<template>
    <el-form label-width="100px">
        <el-form-item label="SKU名称">
            <el-input v-model="skuParams.skuName" placeholder="请您输入SKU名称"></el-input>
        </el-form-item>
        <el-form-item label="价格(元)">
            <el-input v-model="skuParams.price" placeholder="请您输入价格(元)" type="number"></el-input>
        </el-form-item>
        <el-form-item label="重量(克)">
            <el-input v-model="skuParams.weight" placeholder="请您输入重量(克)" type="number"></el-input>
        </el-form-item>
        <el-form-item label="SKU描述">
            <el-input v-model="skuParams.skuDesc" placeholder="请您输入SKU描述" type="textarea"></el-input>
        </el-form-item>
        <el-form-item label="平台属性">
            <el-form :inline="true">
                <el-form-item style="margin-bottom: 10px;" v-for="item in attrArr" :key="item.id" :label="item.attrName">
                    <el-select v-model="item.attrIdAndValueId">
                        <el-option :value="`${item.id}:${attrValue.id}`" v-for="attrValue in item.attrValueList"
                            :key="attrValue.id" :label="attrValue.valueName"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
        </el-form-item>
        <el-form-item label="销售属性">
            <el-form :inline="true">
                <el-form-item style="margin-bottom: 10px;" v-for="item in saleArr" :key="item.id"
                    :label="item.saleAttrName">
                    <el-select v-model="item.saleIdAndValueId">
                        <el-option :value="`${item.id}:${saleAttrValue.id}`"
                            v-for="saleAttrValue in item.spuSaleAttrValueList" :key="saleAttrValue.id"
                            :label="saleAttrValue.saleAttrValueName"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
        </el-form-item>
        <el-form-item label="图片名称">
            <el-table border :data="imgArr" ref="table">
                <el-table-column type="selection" width="80px" align="center"> </el-table-column>
                <el-table-column label="图片">
                    <template #="{ row }">
                        <img :src="row.imgUrl" alt="" style="width: 100px;height: 100px;">
                    </template>
                </el-table-column>
                <el-table-column label="名称" prop="imgName"></el-table-column>
                <el-table-column label="操作">
                    <template #="{ row }">
                        <el-button @click="handler(row)" type="primary" size="small">设置默认</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" size="default" @click="save">保存</el-button>
            <el-button type="primary" size="default" @click="cencel">取消</el-button>
        </el-form-item>
    </el-form>
</template>

<script setup lang="ts">
import { reqAttr } from '@/api/product/attr';
import { reqSpuImageList, reqSpuHasSaleAttr, reqAddSku } from '@/api/product/spu/index'
import { ElMessage } from 'element-plus';
import { ref, reactive } from 'vue';
// 存储平台属性
let attrArr = ref<any>([])
// 销售属性
let saleArr = ref<any>([])
// 照片墙数据
let imgArr = ref<any>([])
// 获取table组件
let table = ref()
// 收集sku的参数
let skuParams = reactive({
    category3Id: '',
    spuId: '',
    tmId: '',
    skuName: '',
    price: '',
    weight: '',
    skuDesc: '',
    skuAttrValueList: [],
    skuSaleAttrValueList: [],
    skuDefaultImg: ''
})
// 取消按钮的回调
const cencel = () => {
    $emit('changeScene', { flag: 0, params: '' })
}

// 当前子组件对外暴露一个方法，父组件调用
const initSkuData = async (c1Id: any, c2Id: any, spu: any) => {
    // 收集数据
    skuParams.category3Id = spu.category3Id
    skuParams.spuId = spu.id
    skuParams.tmId = spu.tmId

    let result = await reqAttr(c1Id, c2Id, spu.category3Id)
    let result1 = await reqSpuHasSaleAttr(spu.id)
    let result2 = await reqSpuImageList(spu.id)

    attrArr.value = result.data
    saleArr.value = result1.data
    imgArr.value = result2.data
}
// 设置默认图片的方法回调
const handler = (row: any) => {
    // 排他思想
    imgArr.value.forEach((item: any) => {
        table.value.toggleRowSelection(item, false)
    });
    table.value.toggleRowSelection(row, true)
    skuParams.skuDefaultImg = row.imgUrl
}

// 保存按钮的回调
const save = async () => {
    // 整理平台属性参数
    skuParams.skuAttrValueList = attrArr.value.reduce((prev: any, next: any) => {
        if (next.attrIdAndValueId) {
            let [attrId, valueId] = next.attrIdAndValueId.split(':')
            prev.push({
                attrId, valueId
            })
        }
        return prev
    }, [])
    // 整理平台销售属性参数
    skuParams.skuSaleAttrValueList = saleArr.value.reduce((prev: any, next: any) => {
        if (next.saleIdAndValueId) {
            let [saleAttrId, saleAttrValueId] = next.saleIdAndValueId.split(':')
            prev.push({
                saleAttrId, saleAttrValueId
            })
        }
        return prev
    }, [])
    let result = await reqAddSku(skuParams)
    if (result.code == 200) {
        ElMessage({ type: 'success', message: '添加SKU成功' })
        $emit('changeScene', { flag: 0, params: '' })
    } else {
        ElMessage({ type: 'error', message: '添加SKU失败' })
    }
}
// 对外暴露子组件的方法
defineExpose({ initSkuData })
// 自定义事件的方法
let $emit = defineEmits(['changeScene'])
</script>

<style scoped></style>